package com.goktech.controller;

import com.goktech.entity.User;
import com.goktech.service.UserService;
import com.goktech.service.imp.userServiceImp;
import com.goktech.util.javaUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @ClassName adminUserController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/5 14:38
 * @Version 1.0
 */

@Controller
@RequestMapping("/admin/User")
public class adminUserController {

    @Autowired
    UserService userService;

    @Autowired
    User user;

    @RequestMapping("/")
    public String UserSelectAll(){
        return "redirect:search/add?yeshu=0";
    }

    @RequestMapping("/search/add")
    public String searchAdd(Integer yeshu,HttpServletRequest request){
        List<User> byAll = null;
        byAll = userService.findByAll(yeshu*5,5);

        if(byAll.size() == 0 && yeshu!=0){
            byAll = userService.findByAll((yeshu-1)*5,5);
        }else{
            yeshu=yeshu+1;
        }
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("users",byAll);
        System.out.println("yeshu:"+yeshu);
        return "admin/userManage";
    }

    @RequestMapping("/search/delete")
    public String searchDelete(Integer yeshu,HttpServletRequest request){
        List<User> byAll = null;
        if(yeshu>1){
            yeshu=yeshu-1;
        }
        byAll = userService.findByAll((yeshu-1)*5,5);
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("users",byAll);
        return "admin/userManage";
    }

    @PostMapping("/update")
    @ResponseBody
    public boolean updateUser(User u){
        int count = 0;
        System.out.println("u:"+u);
        User byUser = userService.findByUser(u);
        System.out.println("byUser:"+byUser);
        if(byUser != null){
            u.setPassword(javaUtil.getResult(u.getPassword()));
            count = userService.updateUser(u);
            if(count != 0){
                System.out.println("count:"+count);
                return true;
            }
        }
        return false;
    }

    @RequestMapping("/delete/{zhanghao}")
    public String deleteZhanghao(@PathVariable("zhanghao")String zhanghao){
        user.setZhanghao(zhanghao);
        userService.deleteUser(user);
        return "redirect:../search/add?yeshu=0";
    }

    @PostMapping("/add")
    @ResponseBody
    public boolean addUser(User u){
        User byUser = userService.findByUser(u);
        if(byUser == null){
            u.setPassword(javaUtil.getResult(u.getPassword()));
            int i = userService.insertUser(u);
            if(i>0){
                return true;
            }
        }
        return false;
    }

    @PostMapping("/select")
    public String select(@Param("select") String select, @Param("text") String text,HttpServletRequest request) throws UnsupportedEncodingException {
        List<User> users = null;
        if("zhanghao".equals(select)){
            user.setZhanghao("%"+text+"%");
            users = userService.findLikeZhanghao(user);
        }else if("username".equals(select)){
            String text1 = new String(text.getBytes("ISO-8859-1"), "utf-8");
            user.setUserName("%"+text1.trim()+"%");
            users = userService.findLikeUserName(user);
        }
        request.setAttribute("users",users);
        request.setAttribute("yeshu",1);
        return "admin/userManage";
    }


    @RequestMapping("/reset")
    @ResponseBody
    public boolean reset(@Param("zhanghao")String zhanghao){
        user.setZhanghao(zhanghao);
        User byZhanghao = userService.findByZhanghao(user);
        if(byZhanghao != null){
            byZhanghao.setPassword(javaUtil.getResult("123456"));
            int i = userService.updateUser(byZhanghao);
            if(i>0){
                return true;
            }
        }
        return false;
    }

    @RequestMapping("/Mreset")
    @ResponseBody
    public boolean Mreset(@Param("zhanghao")String zhanghao){
        String[] split = zhanghao.split(",");
        int number = split.length;
        int number1 = 0;
        for (String s : split) {
            System.out.println(s);
            user.setZhanghao(s);
            User byZhanghao = userService.findByZhanghao(user);
            if(byZhanghao != null){
                byZhanghao.setPassword(javaUtil.getResult("123456"));
                int i = userService.updateUser(byZhanghao);
                if(i>0){
                    number1++;
                }
            }
        }
        if(number==number1){
            return true;
        }
        return false;
    }
}
